﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Spatial;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TreeManagement.Querry
{
    public partial class SearchTree : Form
    {
        public SearchTree()
        {
            InitializeComponent();
        }

        private void SearchTree_Load(object sender, EventArgs e)
        {
            updateControls();
            using (var context = new TreeDBEntities())
            {
                cbbWard.DataSource = context.Wards.ToList();
                cbbNearestWard.DataSource = cbbWard.DataSource;
                cbbRoad.DataSource = context.Roads.ToList();
                cbbTreeType.DataSource = context.TreeTypes.ToList();

                viewPanel1.Wards = context.Wards.ToList();
                viewPanel1.Roads = context.Roads.ToList();
                viewPanel1.Trees = context.Trees.ToList();
                viewPanel1.Refresh();
            }
        }
        private void updateControls()
        {
            if (cbRoad.Checked)
                cbbRoad.Enabled = true;
            else
                cbbRoad.Enabled = false;

            if (cbTreeType.Checked)
                cbbTreeType.Enabled = true;
            else
                cbbTreeType.Enabled = false;

            if (cbWard.Checked)
                cbbWard.Enabled = true;
            else
                cbbWard.Enabled = false;

            if (cbNearestWard.Checked)
                cbbNearestWard.Enabled = true;
            else
                cbbNearestWard.Enabled = false;

            if (cbR.Checked)
                panel1.Enabled = true;
            else
                panel1.Enabled = false;

            if (cbNearest.Checked)
                cbbNearestWard.Enabled = true;
            else
                cbbNearestWard.Enabled = false;
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            viewPanel1.RRect = null;

            using (var context = new TreeDBEntities())
            {
                IQueryable<Tree> querry = context.Trees;
               
                if (cbTreeType.Checked)
                {
                    int treeType = (cbbTreeType.SelectedItem as TreeType).ID;
                    querry = querry.Where(b => b.Type == treeType);
                }
                if (cbRoad.Checked)
                {
                    DbGeometry line = (cbbRoad.SelectedItem as Road).Line;
                    querry = querry.Where(b => b.Point.Distance(line) <= TMConsts.TREE_SIZE);
                }
                if (cbWard.Checked)
                {
                    DbGeometry polygon = (cbbWard.SelectedItem as Ward).Polygon;
                    querry = querry.Where(b => b.Point.Within(polygon));
                }
            if (cbR.Checked)
            {
                DbGeometry geo = context.Trees.Find(Convert.ToInt32( txtID.Text)).Point;
                int R = Convert.ToInt32(txtR.Text);
                List<Tree> trees = context.FindTreeInR(geo, R).ToList<Tree>();
                dataGridView1.DataSource = trees;
                Tuple<Point, int> typle = new Tuple<Point, int>(new Point((int)geo.XCoordinate,(int) geo.YCoordinate), R);
                viewPanel1.RRect = typle;
                viewPanel1.Refresh();
                return;
            }
            if (cbNearest.Checked)
            {
                DbGeometry geo = context.Trees.Find(Convert.ToInt32(txtNearestID.Text)).Point;
                List<Tree> trees = context.FindNearestTreeInR(geo).ToList<Tree>();
                if (trees.Count > 1)
                {
                    List<Tree> visibleData = new List<Tree>();
                    visibleData.Add(trees[1]);
                    dataGridView1.DataSource = visibleData;
                    if (visibleData.Count > 0)
                    {                        
                        viewPanel1.Trees = visibleData;
                        viewPanel1.Refresh();
                    }
                }
                return;
            }
            if (cbNearestWard.Checked)
            {
                DbGeometry wardGeo = (cbbNearestWard.SelectedItem as Ward).Polygon;
                List<Tree> trees = context.FindNearestTreeInWard(wardGeo).ToList<Tree>();
                
                List<Point> wardPointList = TMUtils.getWardPoints(context,(cbbNearestWard.SelectedItem as Ward).ID);
                List<Ward>wards = new List<Ward>();
                wards.Add(cbbNearestWard.SelectedItem as Ward);
                if (trees.Count > 1)
                {
                    List<Tree> visibleData = new List<Tree>();
                    visibleData.Add(trees[1]);
                    dataGridView1.DataSource = visibleData;
                    if (visibleData.Count > 0)
                    {                       
                        viewPanel1.Trees = visibleData;
                        viewPanel1.Wards = wards;
                        viewPanel1.Refresh();
                    }
                }
                return;

            }
            dataGridView1.DataSource = querry.ToList<Tree>();
                viewPanel1.Wards = context.Wards.ToList();
                viewPanel1.Roads = context.Roads.ToList();
                viewPanel1.Trees = context.Trees.ToList();
                viewPanel1.Refresh();
            }
        }

        private void cbTreeType_CheckedChanged(object sender, EventArgs e)
        {
            updateControls();
        }
    }
}
